<?php

namespace app\api\controller\v1\user;

use app\common\controller\BaseController;
use think\Controller;
use think\Request;
use app\api\model\TeacherCourse as TeacherCourseModel;
use app\api\model\TeacherCourseCollect as TeacherCourseCollectModel;

class Collect extends BaseController
{
    protected $noNeedLogin = [''];
    /**
     * 课程收藏
     */
    public function index()
    {
        $userId = $this->userId;
        $size = $this->request->get('size');
        $type = $this->notEmpty('type');
        switch ($type) {
            //曾授课
            case 1:
                $teacherCourse = TeacherCourseModel::ever($userId, $size);
                break;
            //我的收藏
            case 2:
                $teacherCourse = TeacherCourseModel::collect($userId, $size);
                break;
            default:
                $this->errorMsg(10004);
                break;
        }
        $teacherCourse->total() ? $this->success('', [
            'teacher_course' => $teacherCourse->items(),
            'total' => $teacherCourse->total()
        ]) : $this->notFoundData();
    }

    /**
     * 删除收藏
     */
    public function delete()
    {
        $teacherCourseId = $this->notEmpty('teacher_course_id');
        $teacherCourseCollect = TeacherCourseCollectModel::getCollect($teacherCourseId, $this->userId);
        if (!$teacherCourseCollect) {
            $this->errorMsg(10001);
        }
        $res = $teacherCourseCollect->delete();
        $res ? $this->success() : $this->forbidden();
    }
}
